const app = getApp()
const connector = require('../../../utils/connector.js')
let image1 = ""//背景1
let image2 = ""//背景2
let image3 = ""//背景3
let image4 = ""//背景4
let image5 = ""//头像
let imgNum = 0;//进度
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: app.windowWidth,
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    loading:true,
    imgNum:0,
  },
  //点击保存到相册
  baocun: function () {
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册，赶紧晒一下吧~',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#333',
          success: function (res) {
            if (res.confirm) {
              if (that.data.type == 1) {
                let val = app.searchWord
              }

              // /* 该隐藏的隐藏 */
              // that.setData({
              //   maskHidden: false
              // })
            }
          },
          fail: function (res) {
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    userCode(that)
    //06-03
    //"https://data.xuanheng99.com/image/1591181257901bcaf6.jpg"//背景
    //"https://data.xuanheng99.com/image/15911813252612f53a.png"//头部
    //"https://data.xuanheng99.com/image/15911813691526aeb3.png"//中间
    //"https://data.xuanheng99.com/image/1591181401136df4d9.png"//底部

    //06-04
    //"https://data.xuanheng99.com/image/1591236417345edd3f.jpg"//背景
    //"https://data.xuanheng99.com/image/15912368621428bf16.png"//头部
    //"https://data.xuanheng99.com/image/15912369071067ebee.png"//中间
    //"https://data.xuanheng99.com/image/159123688850830e38.png"//底部

    imageCreate(that, "https://data.xuanheng99.com/image/1591181257901bcaf6.jpg", "image1")
    imageCreate(that, "https://data.xuanheng99.com/image/15912368621428bf16.png", "image2")
    imageCreate(that, "https://data.xuanheng99.com/image/15912369071067ebee.png", "image3")
    imageCreate(that, "https://data.xuanheng99.com/image/159123688850830e38.png", "image4")
    // imageCreate(that, "https://data.xuanheng99.com/image/1585232751069172c8.png", "image1")
    // imageCreate(that, "https://data.xuanheng99.com/image/1585232787604c3b4b.png", "image2")
    // imageCreate(that, "https://data.xuanheng99.com/image/1585232837649d0ee8.png", "image3")
    // imageCreate(that, "https://data.xuanheng99.com/image/15852328742058e37d.png", "image4")
  
    // imageCreate(that, wx.getStorageSync("userInfo").coach.picture, "image5")
    // imageCreate1(that)
    // imageCreate2(that)
    // imageCreate3(that)
    // imageCreate4(that)
    // imageCreate5(that)

    let setTime = setInterval(function(){
      console.log(imgNum)
      if (that.data.imgNum == 100){
        clearInterval(setTime)

        createNewImg(that)

      }

    },500)
    // 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    imgNum = 0;
    this.setData({
      imgNum:0
    })
  },


})
//图片转换
function imageCreate(that,src,images) {
  // if (!wx.getStorageSync(images)) {
    wx.getImageInfo({
      src: src, //背景
      success: function (res) {
        console.log(res)
        // image1 = res.path
        imgNum += 15
        that.setData({
          imgNum: imgNum
        })
        wx.setStorage({
          key: images,
          data: res.path,
        });
      }
    })
  // } else {
  //   imgNum += 10
  //   that.setData({
  //     imgNum: imgNum
  //   })
  // }
}

//生成图片
function createNewImg(that){
    var widthBl = 25
    let width = app.windowWidth

    let height = app.windowWidth * 1.7
    var context = wx.createCanvasContext('mycanvas');
    context.setFillStyle("#fff")
    context.fillRect(0, 0, width + 40, width * 1.7)
    context.drawImage(wx.getStorageSync("image1"), 15, 15, width - 30, height - 100);
    context.drawImage(wx.getStorageSync("image2"), 15, 20, width - 30, 80);
    context.drawImage(wx.getStorageSync("image3"), 15, 90, width - 30, 100);
    context.drawImage(wx.getStorageSync("image4"), 25, height - 150 , width - 50, 70);
    // context.drawImage(wx.getStorageSync("image5"), ( width - 90)/2, 210, 90, 90);


    // context.drawImage(wx.getStorageSync("image2"), 15, 280, width - 30, 90);
    context.drawImage(wx.getStorageSync("userCode"),( width - 155)/2, height - 320 , 155, 155);

  //   //写字
  //   context.setFontSize(15);
  //   context.setFillStyle('#fff');
  //   context.setTextAlign('center');
  //   let titles = wx.getStorageSync("userInfo").coach.name
  // if (titles.length > 7){
  //   titles = titles.slice(0,6) + "..."
  // }
  // context.fillText(titles, width / 2, 335)

    context.stroke();
    context.draw();
    //   //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'mycanvas',
        success: function (res) {

          console.log(res)
          var tempFilePath = res.tempFilePath;
          that.setData({
            imagePath: tempFilePath,
            canvasHidden: true,
            loading: false,
          });
          wx.setStorage({
            key: "imagePaths",
            data: tempFilePath,
          });
        },
        fail: function (res) {
        }
      });
    }, 1000);
  
}
//获取小程序码
function userCode(that){
  let obj={
    path:"pages/first/first"
  }
  // if (!wx.getStorageSync("userCode")) {
    connector.getFunc(connector.connector().userCode, obj, "post", app.tokens, function (res) {
      console.log(res)
      if (res.statusCode == 200) {
        console.log(res.data)
        // that.setData({
        //   orderOnes: res.data
        // })
        imgNum += 20
        that.setData({
          imgNum: imgNum
        })
        console.log(res.data.result.path)
        wx.getImageInfo({
          src: res.data.result.path, //小程序码
          success: function (res) {
            console.log(res)
            // image1 = res.path
            imgNum += 20
            that.setData({
              imgNum: imgNum
            })
            wx.setStorage({
              key: "userCode",
              data: res.path,
            });
          }
        })

      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
  // }else{
  //   imgNum += 40
  //   that.setData({
  //     imgNum: imgNum
  //   })
  // }
}